import {
  Body,
  Controller,
  Get,
  HttpStatus,
  Post,
  Req,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import {
  BaseApiErrorResponse,
  // SwaggerBaseApiResponse,
} from '@/shared/dtos/base-api-response.dto';
import { AuthService } from '../services/auth.service';
import {
  RegisterCodeDto,
  RegisterSMSDto,
  UserInfoDto,
  TokenDto,
} from '../dtos/auth.dto';
import { LoginDto } from './../dtos/login.dto';

@ApiTags('认证鉴权')
@Controller('auth')
export class AuthController {
  constructor(private authService: AuthService) {}

  @ApiOperation({
    summary: '用户登录',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    type: TokenDto,
  })
  @ApiResponse({
    status: HttpStatus.NOT_FOUND,
    type: BaseApiErrorResponse,
  })
  @Post('login')
  async login(@Body() loginDto: LoginDto): Promise<any> {
    return this.authService.login(loginDto);
  }

  @ApiOperation({
    summary: '当前用户信息',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    type: UserInfoDto,
  })
  @ApiBearerAuth()
  @ApiResponse({
    status: HttpStatus.NOT_FOUND,
    type: BaseApiErrorResponse,
  })
  @Get('info')
  @UseGuards(AuthGuard('jwt'))
  async info(@Req() req: any): Promise<any> {
    const data = await this.authService.info(req.user.id);
    return { data };
  }

  @ApiOperation({
    summary: '短信验证码',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    // type:
  })
  @ApiResponse({
    status: HttpStatus.NOT_FOUND,
    type: BaseApiErrorResponse,
  })
  @Post('registerCode')
  async registerCode(@Body() registerCodeDto: RegisterCodeDto): Promise<any> {
    const code = await this.authService.registerCode(registerCodeDto);
    return {
      msg: '验证码已生成',
      data: { code },
    };
  }

  @ApiOperation({
    summary: '图形验证码',
  })
  @Get('captcha') //当请求该接口时，返回一张随机图片验证码
  async getCaptcha() {
    const data = await this.authService.getCaptcha();
    return {
      data,
    };
  }

  @ApiOperation({
    summary: '短信用户注册/登录',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    // type: SwaggerBaseApiResponse('RegisterSMSDtoAll', RegisterSMSDto),
  })
  @ApiResponse({
    status: HttpStatus.NOT_FOUND,
    type: BaseApiErrorResponse,
  })
  @Post('registerBySms')
  async registerBySms(@Body() registerDTO: RegisterSMSDto): Promise<any> {
    return this.authService.registerBySms(registerDTO);
  }
}
